//
//  MyActivityViewController.m
//  UI14_豆瓣电影更新版
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MyActivityViewController.h"
#import "dataBaseHandle.h"
#import "ActivityModel.h"
@interface MyActivityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation MyActivityViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 标题(属性传值)
    self.navigationItem.title = _str;
    // 数据处理
    [self handle];
}
// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
// cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = _arr[indexPath.row];
    
    return cell;
}
// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // 先执行父类的方法
    [super setEditing:editing animated:animated];
    // 设置tableView的编辑状态
    [_tableView setEditing:editing animated:animated];
}
// 设置哪些行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // 默认所有行都可以
    return YES;
}
// 设置cell的编辑模式(默认删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        ActivityModel *model = [ActivityModel activity];
        model.title = _arr[indexPath.row];
        [_arr removeObjectAtIndex:indexPath.row];
        [[dataBaseHandle sharedDataBaseHandle] deleteModel:model];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

    }
}



// 数据处理
- (void)handle {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 注册重用池
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //从数据库中取值(查找)
    self.arr = [[dataBaseHandle sharedDataBaseHandle] selectModel];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
